﻿//////////////////////////////////////////
/// 提供界面, 方便对客户端发送一些内容
//////////////////////////////////////////
#include "ShowWidget.h"
#include <QFileInfo>
#include <QHBoxLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QScrollBar>
#include <QTextEdit>
#include "WebSocket.h"

// 初始化静态成员变量
ShowWidget* ShowWidget::instance = nullptr;

ShowWidget* ShowWidget::get_instance()
{
    if (instance == nullptr) {
        if (instance == nullptr) {
            instance = new ShowWidget();
        }
    }
    return instance;
}

ShowWidget::ShowWidget(QWidget* parent)
    : QWidget(parent)
{
    this->setWindowTitle("测试服务器");
    init_ui();
    connect_slots();
}

void ShowWidget::init_ui()
{
    this->resize(1000, 800);

    // 创建主垂直布局
    QVBoxLayout* main_layout = new QVBoxLayout(this);

    // 创建上半部分的滚动区域
    QScrollArea* scroll_area = new QScrollArea(this);
    scroll_area->setWidgetResizable(true);  // 允许内部部件自动调整大小

    // 创建文本编辑框用于日志输出
    log_output = new QTextEdit(this);
    log_output->setReadOnly(true);       // 设置为只读
    scroll_area->setWidget(log_output);  // 将文本编辑框设置为滚动区域的部件
    log_output->setStyleSheet("font-size: 14px;");
    // 创建下半部分的按钮区域
    QWidget*     button_widget = new QWidget(this);
    QGridLayout* button_layout = new QGridLayout(button_widget);

    // 添加清除按钮
    clear_btn = new QPushButton("清空日志");
    clear_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(clear_btn, 0, 0);

    // 添加发送按钮
    send_btn = new QPushButton("发送消息");
    send_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(send_btn, 0, 1);

    // 添加删除好友按钮
    del_btn = new QPushButton("删除好友");
    del_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(del_btn, 0, 2);

    // 添加好友申请按钮
    friend_apply_btn = new QPushButton("发送好友申请");
    friend_apply_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(friend_apply_btn, 1, 0);

    // 添加同意好友申请按钮
    ac_friend_apply_btn = new QPushButton("同意好友申请");
    ac_friend_apply_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(ac_friend_apply_btn, 1, 1);

    // 添加拒绝好友申请按钮
    rej_friend_apply_btn = new QPushButton("拒绝好友申请");
    rej_friend_apply_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(rej_friend_apply_btn, 1, 2);
       
    // 添加创建群聊按钮
    create_group_btn = new QPushButton("创建群聊");
    create_group_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(create_group_btn, 2, 0);
    
    // 添加推送图片消息按钮
    send_image_btn = new QPushButton("发送图片消息");
    send_image_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(send_image_btn, 2, 1);

    // 添加推送文件消息按钮
    send_file_btn = new QPushButton("发送文件消息");
    send_file_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(send_file_btn, 2, 2);

    // 添加推送语音消息按钮
    send_speech_btn = new QPushButton("发送语音消息");
    send_speech_btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button_layout->addWidget(send_speech_btn, 3, 0);
    // 将滚动区域和按钮区域添加到主布局
    main_layout->addWidget(scroll_area, 3);    // 滚动区域占3份空间
    main_layout->addWidget(button_widget, 1);  // 按钮区域占1份空间
    
    this->setLayout(main_layout);
}

void ShowWidget::connect_slots()
{
    /* 点击清空消息按钮 */
    connect(clear_btn, &QPushButton::clicked, this, [this] {
        log_output->clear();
    });

    /* 点击发送消息按钮 */
    connect(send_btn, &QPushButton::clicked, this, &ShowWidget::send_btn_clicked);

    /* 点击删除好友按钮 */
    connect(del_btn, &QPushButton::clicked, this, [this] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->delete_friend();
    });

    /* 点击好友申请按钮 */
    connect(friend_apply_btn, &QPushButton::clicked, this, [this] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->send_friend_apply();
    });

    /* 点击同意好友申请按钮 */
    connect(ac_friend_apply_btn, &QPushButton::clicked, this, [=] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->send_friend_apply_process(true);
    });

    /* 点击拒绝好友申请按钮 */
    connect(rej_friend_apply_btn, &QPushButton::clicked, this, [=] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->send_friend_apply_process(false);
    });

    /* 点击创建群聊按钮 */
    connect(create_group_btn, &QPushButton::clicked, this, [this] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->send_create_group();
    });

    // 点击发送图片消息按钮
    connect(send_image_btn, &QPushButton::clicked, this, [this] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->send_image_message();  
    });

    // 点击发送文件消息按钮
    connect(send_file_btn, &QPushButton::clicked, this, [this] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->send_file_message();  
    });

    // 点击发送语音消息按钮
    connect(send_speech_btn, &QPushButton::clicked, this, [this] {
        WebSocket* ws = WebSocket::get_instance();
        emit       ws->send_speech_message();  
    });
}

/// @brief 将消息添加日志消息(不带日志级别)
void ShowWidget::add_log_message(const QString& message, const char* file, int line)
{
    QString logMessage;

    // 如果有文件信息，添加到日志中
    if (file && line > 0) {
        // 只取文件名，不取完整路径
        QString fileName = QFileInfo(file).fileName();
        logMessage       = QString("[%1:%2] %3").arg(fileName).arg(line).arg(message);
    } else {
        logMessage = message;
    }

    // 添加到日志输出
    log_output->append(logMessage);

    // 自动滚动到底部
    QScrollBar* scrollbar = log_output->verticalScrollBar();
    scrollbar->setValue(scrollbar->maximum());
}

/// @brief 将消息添加日志消息(带日志级别)
void ShowWidget::add_log_message_level(const QString& message, LOG_LEVEL level, const char* file, int line)
{
    const QString level_str = [level]() {
        switch (level) {
        case LOG_LEVEL::INFO:
            return "[INFO]: ";
        case LOG_LEVEL::WARNING:
            return "[WARNING]: ";
        case LOG_LEVEL::FATAL:
            return "[FATAL]: ";
        default:
            return "[UNKNOW]: ";
        }
    }();

    QString logMessage;

    // 如果有文件信息，添加到日志中
    if (file && line > 0) {
        // 只取文件名，不取完整路径
        QString fileName = QFileInfo(file).fileName();
        logMessage       = QString("[%1:%2][%3] %4").arg(fileName).arg(line).arg(level_str).arg(message);
    } else {
        logMessage = level_str + message;
    }

    // 添加到日志输出
    log_output->append(logMessage);

    // 自动滚动到底部
    QScrollBar* scrollbar = log_output->verticalScrollBar();
    scrollbar->setValue(scrollbar->maximum());
}

/// @brief 处理发送按钮点击事件
void ShowWidget::send_btn_clicked()
{
    WebSocket* ws = WebSocket::get_instance();
    emit       ws->send_txt_message();
}
